﻿// ******************************************************************************
// Developed by Oguz Demirel
// Date: March 9, 2010
// ******************************************************************************
namespace BulkListActions
{
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Security;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.WebPartPages;
    using System;
    using System.Globalization;
    using System.Security.Permissions;
    using System.Web;
    using System.Web.UI;
    using System.Collections.Specialized;
    using System.Net;
    using System.Web.UI.WebControls;

    public class Helper
    {
        // This recursive method finds list view web part on SharePoint page
        public static ListViewWebPart FindListView(Control parent)
        {
            ListViewWebPart part = parent as ListViewWebPart;
            if (part != null)
            {
                return part;
            }
            if (parent.Parent == null)
            {
                return null;
            }
            return FindListView(parent.Parent);
        }

        // This method registers jQuery to the page
        public static void RegisterScript(Page page, string clientID, ListViewWebPart lvwp)
        {
            if (!page.ClientScript.IsClientScriptIncludeRegistered("ListItemSelection"))
            {
                page.ClientScript.RegisterClientScriptInclude("ListItemSelection", "/_layouts/ListItemSelection.js");
            }

            string startupScript = string.Format(CultureInfo.InvariantCulture, "$(function(){{ListItemSelection_Init('{0}', 'WebPart{1}');}});",
                                                clientID,
                                                lvwp.Qualifier);

            page.ClientScript.RegisterStartupScript(typeof(SelectItemsAction), clientID, startupScript, true);
        
        }

        // This method checks query string parameters
        public static void CheckQueryString(NameValueCollection queryString)
        {
            // Parse and check qs parameters
            string strList = queryString["List"];
            if (string.IsNullOrEmpty(strList))
                throw new WebException("Required query string parameter 'List' is not defined.");

            string strView = queryString["View"];
            if (string.IsNullOrEmpty(strView))
                throw new WebException("Required query string parameter 'View' is not defined.");

            string referrer = queryString["referrer"];
            if (string.IsNullOrEmpty(referrer))
                throw new WebException("Required query string parameter 'referrer' is not defined.");
        
        }

    }

}
